﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Arcana.Contract.Method.Returns
{
    /// <summary>
    /// Represents the base class for all contracts that validate method return value.
    /// </summary>
    /// <remarks>
    /// <b>Note to Implementers:</b> You have to override <see cref="ValidateValue"/> method. This method will throw exception
    /// using <see cref="ThrowException"/> if validation fails. <br />
    /// <b>Note to implementers: </b> Proper roles should be applied by each implementing class. See
    /// <see cref="Roles"/> for more information on this topic. <br />
    /// <b>Note: </b> The default exception to throw is <see cref="System.InvalidOperationException"/>. To change the exception type
    /// please set the named property, <see cref="ContractBase.Exception"/>.
    /// </remarks>
    [Serializable]
    public abstract class ContractBase : PostSharp.Aspects.OnMethodBoundaryAspect, IContract
    {
        private Type _exception;

        /// <summary>
        /// Initializes a new instance of <b>ContractBase</b>.
        /// </summary>
        /// <remarks>
        /// This constructor initializes a new instance of <b>ContractBase</b>, and sets the default exception occuring
        /// to <see cref="System.InvalidOperationException"/>. The type of exception to throw if contract validation fails can
        /// be set using the named property <see cref="Exception"/>. <br />
        /// <b>Note to Implementers:</b> You have to override <see cref="ValidateValue"/> method. This method will throw exception
        /// using <see cref="ThrowException"/> if validation fails.
        /// </remarks>
        public ContractBase()
        {
            _exception = typeof(System.InvalidOperationException);
        }

        /// <summary>
        /// Occurs when the mehtod successfuly exits the execution, and no exception has occured.
        /// </summary>
        /// <remarks>
        /// <b>Note to Implementers:</b> You have to override <see cref="ValidateValue"/> method. This method will throw exception
        /// using <see cref="ThrowException"/> if validation fails.
        /// </remarks>
        /// <param name="args">Method execution context.</param>
        public override void OnSuccess(PostSharp.Aspects.MethodExecutionArgs args)
        {
            if (!ValidateValue(args.ReturnValue))
            {
                ThrowException();
            }
        }

        /// <summary>
        /// Performs validation on the retunr value.
        /// </summary>
        /// <param name="value">The return value that should be validated.</param>
        /// <returns><b>true</b> if <i>value</i> is validated, otherwise <b>false</b>.</returns>
        protected abstract bool ValidateValue(object value);

        /// <summary>
        /// Throws exception appointed by <see cref="ContractBase.Exception"/>
        /// </summary>
        protected virtual void ThrowException()
        {
            throw Exception.GetConstructor(new Type[] { }).Invoke(new object[] { }) as System.Exception;
        }

        /// <summary>
        /// Represents type of exception to throw if contract validation fails.
        /// </summary>
        /// <value>
        /// The type of exception to throw, if the contract validation fails.
        /// </value>
        /// <remarks>
        /// This value must inherit from <see cref="System.Exception"/> class. This value is set to
        /// <see cref="System.ArgumentException"/> by default.
        /// </remarks>
        /// <exception cref="ArgumentNullException"><i>value</i> is <b>null</b>.</exception>
        /// <exception cref="ArgumentException"><i>value</i> does not inherit from <see cref="System.Exception"/></exception>
        public Type Exception
        {
            get
            {
                return _exception;
            }
            set
            {
                if (value == null) throw new ArgumentNullException();
                if (!value.IsSubclassOf(typeof(System.Exception))) throw new ArgumentException();
                _exception = value;
            }
        }
    }
}
